<template>
  <div style="width: 80%;margin: 20px auto">
    <h1>欢迎使用图书管理系统</h1>

    <el-collapse v-model="activeNames">
      <el-alert title="下拉可修改对应值" :closable="false" type="info" style="line-height: 12px;"></el-alert>

      <el-collapse-item v-if="username" :title="username" name="1">
        <div>
          <el-form :inline="true" :model="userInfo" class="demo-form-inline">
            <el-form-item label="用户名">
              <el-input v-model="userInfo.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="update">修改用户名</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
      <el-collapse-item v-if="email" :title="email" name="2">
        <div>
          <el-form :inline="true" :model="userInfo" class="demo-form-inline">
            <el-form-item label="邮箱">
              <el-input v-model="userInfo.email" placeholder="邮箱"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="update">修改邮箱</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
      <el-collapse-item v-if="phone" :title="phone" name="3">
        <div>
          <el-form :inline="true" :model="userInfo" class="demo-form-inline">
            <el-form-item label="手机号码">
              <el-input v-model="userInfo.phone" placeholder="手机号码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="update">修改手机号码</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
      <el-collapse-item title="修改密码" name="4">
        <div>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                   class="demo-ruleForm">
            <el-form-item label="当前密码" prop="currentPass">
              <el-input type="password" v-model="ruleForm.currentPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="pass">
              <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
      <el-card  v-if="userInfo.lastLogin&&userInfo.created" style="margin-top: 20px" class="box-card">
        <p class="userInfo-p" v-if="userInfo.lastLogin">
          <span>最后登录时间：{{ userInfo.lastLogin }}</span>
        </p>
        <p class="userInfo-p" v-if="userInfo.created">
          <span>账号注册时间：{{ userInfo.created }}</span>
        </p>
      </el-card>

    </el-collapse>
  </div>
</template>

<script>

export default {
  name: "Index",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      activeNames: [],
      userInfo: null,
      username: null,
      email: null,
      phone: null,
      ruleForm: {
        password: '',
        checkPass: '',
        currentPass: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 18 个字符', trigger: 'blur' },
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        currentPass: [
          { required: true, message: '请输入当前密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    update: function () {
      const loading = this.$loading({
        lock: true,
        text: 'loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.$axios.put("/sys/user", this.userInfo).then(res => {
        loading.close();
        this.activeNames = [];
        this.$message({
          showClose: true, message: '恭喜你，操作成功', type: 'success', duration: 2000, onClose: () => {
            this.getUserInfo();
          }
        })
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          const loading = this.$loading({
            lock: true,
            text: 'loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          this.$axios.post("/sys/user/password", this.ruleForm).then(res => {
            loading.close();
            this.activeNames = [];
            _this.$alert(res.data.msg==="success"?"修改密码成功":res.data.msg, '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$refs[formName].resetFields();
              }
            })
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getUserInfo() {
      this.$axios.get("/sys/user/info").then(res => {
            this.userInfo = res.data.data;
            this.username = "用户名：" + res.data.data.username;
            this.email = "邮箱：" + res.data.data.email;
            this.phone = "手机号码：" + res.data.data.phone;

          }
      );
    }
  },
  created() {
    this.getUserInfo();
  }
}
</script>

<style scoped>

</style>
